package com.mazaiting.redeye.module.msg

import android.text.TextUtils
import cn.jpush.im.android.api.content.TextContent
import cn.jpush.im.android.api.enums.ContentType
import cn.jpush.im.android.api.model.Conversation
import cn.jpush.im.android.api.model.UserInfo
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.mazaiting.emotion.util.EmotionUtil
import com.mazaiting.emotion.util.SpanStringUtil
import com.mazaiting.log.L
import com.mazaiting.redeye.R
import com.mazaiting.redeye.utils.TimeUtil

/***
 *
 *
 *                                                    __----~~~~~~~~~~~------___
 *                                   .  .   ~~//====......          __--~ ~~
 *                   -.            \_|//     |||\\  ~~~~~~::::... /~
 *                ___-==_       _-~o~  \/    |||  \\            _/~~-
 *        __---~~~.==~||\=_    -_--~/_-~|-   |\\   \\        _/~
 *    _-~~     .=~    |  \\-_    '-~7  /-   /  ||    \      /
 *  .~       .~       |   \\ -_    /  /-   /   ||      \   /
 * /  ____  /         |     \\ ~-_/  /|- _/   .||       \ /
 * |~~    ~~|--~~~~--_ \     ~==-/   | \~--===~~        .\
 *          '         ~-|      /|    |-~\~~       __--~~
 *                      |-~~-_/ |    |   ~\_   _-~            /\
 *                           /  \     \__   \/~                \__
 *                       _--~ _/ | .-~~____--~-/                  ~~==.
 *                      ((->/~   '.|||' -_|    ~~-/ ,              . _||
 *                                 -_     ~\      ~~---l__i__i__i--~~_/
 *                                 _-~-__   ~)  \--______________--~~
 *                               //.-~~~-~_--~- |-------~~~~~~~~
 *                                      //.-~~~--\
 *                               神兽保佑
 *                              代码无BUG!
 * @author mazaiting
 * @date 2019-07-26
 * @description 消息列表适配器
 */
class MsgAdapter internal constructor() : BaseQuickAdapter<Conversation, BaseViewHolder>(R.layout.item_msg) {
    override fun convert(helper: BaseViewHolder?, item: Conversation?) {
        // 获取用户信息
        val userInfo: UserInfo = item?.targetInfo as UserInfo

//        L.d("userInfo: $userInfo")
//        L.d("item.latestMessage.content: ${item.latestMessage.content}")

        // 设置用户名
        helper?.setText(R.id.item_msg_title, userInfo.userName)
        // 设置点击事件
//        helper?.addOnClickListener(R.id.item_msg_title)
        // 获取最后一条消息
        val latestMessage = item.latestMessage

        if (null != latestMessage) {

            // 设置最后一条消息
            if (latestMessage.contentType == ContentType.text) {
                // 文本消息
                val content = latestMessage.content as TextContent

                // 设置最后一条消息
                val latestText = content.text
                if (TextUtils.isEmpty(latestText)) {
                    helper?.setText(R.id.item_msg_content, latestText)
                } else {
                    helper?.setText(R.id.item_msg_content,
                        SpanStringUtil.getEmotionContent(
                            mContext,
                            EmotionUtil.EMOTION_CLASSIC_TYPE,
                            helper.getView(R.id.item_msg_content),
                            latestText
                        )
                    )
                }
            }

            // 设置时间
            // 获取创建时间
            val createTime = latestMessage.createTime
            helper?.setText(R.id.item_time, TimeUtil.getTime(createTime))

            // 设置未读消息数
            val unReadMsgCnt =
                if (item.unReadMsgCnt > 0) {
                    item.unReadMsgCnt.toString()
                } else {
                    ""
                }
            helper?.setText(R.id.item_unread_msg_count, unReadMsgCnt)
        } else {
            // 设置最后一条消息
            if (item.latestType == ContentType.text) {
                // 设置最后一条消息
//                helper?.setText(R.id.item_msg_content, item.latestText)
                val latestText = item.latestText
                if (TextUtils.isEmpty(latestText)) {
                    helper?.setText(R.id.item_msg_content, latestText)
                } else {
                    helper?.setText(R.id.item_msg_content,
                        SpanStringUtil.getEmotionContent(
                            mContext,
                            EmotionUtil.EMOTION_CLASSIC_TYPE,
                            helper.getView(R.id.item_msg_content),
                            latestText
                        )
                    )
                }
            }

            // 设置时间
            // 获取创建时间
            helper?.setText(R.id.item_time, TimeUtil.getTime(item.lastMsgDate))

            // 设置未读消息数
            val unReadMsgCnt =
                if (item.unReadMsgCnt > 0) {
                    item.unReadMsgCnt.toString()
                } else {
                    ""
                }
            helper?.setText(R.id.item_unread_msg_count, unReadMsgCnt)
        }
    }

}
